package com.buildings.view;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.buildings.R;
import com.buildings.activity.table.CraterActivity;
import com.buildings.activity.table.DrillHoleActivity;
import com.buildings.activity.table.FaultSvyPointActivity;
import com.buildings.activity.table.GeoGeomorphySvyPointActivity;
import com.buildings.activity.table.GeochemicalSvyLineActivity;
import com.buildings.activity.table.GeochemicalSvyPointActivity;
import com.buildings.activity.table.GeologicalSvyLineActivity;
import com.buildings.activity.table.GeologicalSvyPlanningLineActivity;
import com.buildings.activity.table.GeologicalSvyPlanningPtActivity;
import com.buildings.activity.table.GeologicalSvyPointActivity;
import com.buildings.activity.table.GeomorStationActivity;
import com.buildings.activity.table.GeomorphySvyLineActivity;
import com.buildings.activity.table.GeomorphySvyPointActivity;
import com.buildings.activity.table.GeomorphySvyReProfActivity;
import com.buildings.activity.table.GeomorphySvyRegionActivity;
import com.buildings.activity.table.GeomorphySvySamplePointActivity;
import com.buildings.activity.table.GeophySvyLineActivity;
import com.buildings.activity.table.GeophySvyPointActivity;
import com.buildings.activity.table.LavaActivity;
import com.buildings.activity.table.SamplePointActivity;
import com.buildings.activity.table.StratigraphySvyPointActivity;
import com.buildings.activity.table.TanCaodianActivity;
import com.buildings.activity.table.VolcanicSamplePointActivity;
import com.buildings.activity.table.VolcanicSvyPointActivity;
import com.buildings.adapter.SelectType2Adapter;
import com.buildings.adapter.SelectTypeAdapter;
import com.buildings.bean.AddressBean;
import com.buildings.bean.TableTypeListBean;
import com.buildings.util.DataUtils;
import com.google.gson.Gson;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class FragmentDialog extends DialogFragment implements View.OnClickListener {
    private static final String GET_SAFETY_GRADE_BEAN = "queryunitBean";
    View mView;
    private RecyclerView recycle1;
    private RecyclerView recycle2;
    private SelectTypeAdapter adapter;
    private SelectType2Adapter adapter2;
    private ImageView iv_back;
    private TextView tv_title;
    private TextView tv_right_text;
    private ArrayList<TableTypeListBean> jsonBean;
    private TextView clea, queding;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.custom_dialog);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.dialog_select_type, container, false);
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        init();
        initData();
        return mView;
    }

    private double x;
    private double y;
    private String projectId;
    private String projectName;
    private String taskName;
    private String taskId;

    private void init() {
        x = getArguments().getDouble("x");
        y = getArguments().getDouble("y");
        projectId = getArguments().getString("projectId");
        projectName = getArguments().getString("projectName");
        taskName = getArguments().getString("taskName");
        taskId = getArguments().getString("taskId");
        recycle1 = mView.findViewById(R.id.recycle1);
        recycle2 = mView.findViewById(R.id.recycle2);
        iv_back = mView.findViewById(R.id.iv_back);
        tv_title = mView.findViewById(R.id.tv_title);
        tv_right_text = mView.findViewById(R.id.tv_right_text);
        iv_back.setVisibility(View.GONE);
        tv_title.setText("选择类型");
        tv_right_text.setText("完成");
        tv_right_text.setOnClickListener(this);

    }

    List<TableTypeListBean.DataBean> list = new ArrayList<>();

    private void initData() {
        String JsonData = DataUtils.getAddressJson(getActivity(), "dixingTypeNew.json");
        jsonBean = parseData(JsonData);//用Gson 转成实体
        adapter = new SelectTypeAdapter(getActivity(), jsonBean);
        LinearLayoutManager linearManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recycle1.setLayoutManager(linearManager);
        recycle1.setAdapter(adapter);
        adapter.setOnItemClickListener(new SelectTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (list.size() > 0) {
                    for (TableTypeListBean.DataBean dataBean : list) {
                        dataBean.setCheck(false);
                    }
                }
                list.clear();
                list.addAll(jsonBean.get(position).getData());
                adapter2.notifyDataSetChanged();
                if (list.size() > 0)
                    selectId = list.get(0).getId();
            }
        });

        adapter2 = new SelectType2Adapter(getActivity(), list);
        LinearLayoutManager linearManager1 = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recycle2.setLayoutManager(linearManager1);
        recycle2.setAdapter(adapter2);
        adapter2.setOnItemClickListener(new SelectType2Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                selectId = list.get(position).getId();
            }
        });
    }

    private String selectId = "1";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right_text:
                jumpTableActivity(selectId);
                break;
        }
    }

    private void jumpTableActivity(String selectId) {
        dismiss();
        Intent intent;
        switch (selectId) {
            case "1":
                intent = new Intent(getActivity(), GeologicalSvyPlanningLineActivity.class);
                break;
            case "2":
                intent = new Intent(getActivity(), GeologicalSvyPlanningPtActivity.class);
                break;
            case "3":
                intent = new Intent(getActivity(), FaultSvyPointActivity.class);
                break;
            case "4":
                intent = new Intent(getActivity(), GeoGeomorphySvyPointActivity.class);
                break;
            case "5":
                intent = new Intent(getActivity(), GeologicalSvyLineActivity.class);
                break;
            case "6":
                intent = new Intent(getActivity(), GeologicalSvyPointActivity.class);
                break;
            case "7":
                intent = new Intent(getActivity(), StratigraphySvyPointActivity.class);
                break;
            case "8":
                intent = new Intent(getActivity(), TanCaodianActivity.class);
                break;
            case "9":
                intent = new Intent(getActivity(), GeomorphySvyLineActivity.class);
                break;
            case "10":
                intent = new Intent(getActivity(), GeomorphySvyPointActivity.class);
                break;
            case "11":
                intent = new Intent(getActivity(), GeomorphySvyRegionActivity.class);
                break;
            case "12":
                intent = new Intent(getActivity(), GeomorphySvyReProfActivity.class);
                break;
            case "13":
                intent = new Intent(getActivity(), GeomorphySvySamplePointActivity.class);
                break;
            case "14":
                intent = new Intent(getActivity(), GeomorStationActivity.class);
                break;
            case "15":
                intent = new Intent(getActivity(), DrillHoleActivity.class);
                break;
            case "16":
                intent = new Intent(getActivity(), SamplePointActivity.class);
                break;
            case "17":
                intent = new Intent(getActivity(), GeophySvyLineActivity.class);
                break;
            case "18":
                intent = new Intent(getActivity(), GeophySvyPointActivity.class);
                break;
            case "19":
                intent = new Intent(getActivity(), GeochemicalSvyLineActivity.class);
                break;
            case "20":
                intent = new Intent(getActivity(), GeochemicalSvyPointActivity.class);
                break;
            case "21":
                intent = new Intent(getActivity(), CraterActivity.class);
                break;
            case "22":
                intent = new Intent(getActivity(), LavaActivity.class);
                break;
            case "23":
                intent = new Intent(getActivity(), VolcanicSamplePointActivity.class);
                break;
            default:
                intent = new Intent(getActivity(), VolcanicSvyPointActivity.class);
                break;
        }
        intent.putExtra("x", x);
        intent.putExtra("y", y);
        intent.putExtra("type", selectId);
        intent.putExtra("taskId", taskId);
        intent.putExtra("projectId", projectId);
        intent.putExtra("projectName", projectName);
        intent.putExtra("taskName", taskName);
        startActivity(intent);
    }

    public static FragmentDialog newInstance(double x, double y, String taskId
            , String projectId,String projectName,String taskName) {
        FragmentDialog fragmentB = new FragmentDialog();
        Bundle bundle = new Bundle();
        bundle.putDouble("x", x);
        bundle.putDouble("y", y);
        bundle.putString("taskId", taskId);
        bundle.putString("projectId", projectId);
        bundle.putString("projectName", projectName);
        bundle.putString("taskName", taskName);
        fragmentB.setArguments(bundle);
        return fragmentB;
    }

    public ArrayList<TableTypeListBean> parseData(String result) {//Gson 解析
        ArrayList<TableTypeListBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                TableTypeListBean entity = gson.fromJson(data.optJSONObject(i).toString(), TableTypeListBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return detail;
    }
}
